package jmu.zzh.alumnus.business.service;

import cn.hutool.core.bean.BeanUtil;
import jmu.zzh.alumnus.common.response.PageResponse;
import jmu.zzh.alumnus.business.entity.News;
import jmu.zzh.alumnus.business.entity.NewsExample;
import jmu.zzh.alumnus.business.mapper.NewsMapper;
import jmu.zzh.alumnus.business.request.NewsListRequest;
import jmu.zzh.alumnus.business.request.NewsRequest;
import jmu.zzh.alumnus.business.response.NewsResponse;
import jmu.zzh.alumnus.common.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsService {
    private static final Logger log = LoggerFactory.getLogger(NewsService.class);
    
    @Autowired
    private NewsMapper newsMapper;
    
    @Autowired
    private SnowFlake snowFlake;

    public PageResponse<NewsResponse> list(NewsListRequest request) {
        NewsExample newsExample = new NewsExample();
        newsExample.setOrderByClause("id desc");

        // 处理搜索条件
        NewsExample.Criteria criteria = newsExample.createCriteria();
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            String keyword = "%" + request.getKeyword() + "%";
            criteria.andTitleLike(keyword);
        }

        PageHelper.startPage(request.getPage(), request.getSize());
        log.info("当前页码 current_page = {}", request.getPage());
        log.info("每页记录数 per_page = {}", request.getSize());

        // 修改为selectByExampleWithBLOBs以获取完整内容
        List<News> newsList = newsMapper.selectByExampleWithBLOBs(newsExample);

        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        log.info("总记录数 total = {}", pageInfo.getTotal());
        log.info("总页数 pages = {}", pageInfo.getPages());

        List<NewsResponse> list = BeanUtil.copyToList(newsList, NewsResponse.class);

        PageResponse<NewsResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);

        return pageResponse;
    }

    public NewsResponse getById(Long id) {
        // 修改为selectByPrimaryKey以获取完整内容
        News news = newsMapper.selectByPrimaryKey(id);
        if (news == null) {
            return null;
        }
        return BeanUtil.copyProperties(news, NewsResponse.class);
    }

    public void add(NewsRequest request) {
        News news = BeanUtil.copyProperties(request, News.class);
        // 使用雪花算法生成ID
        news.setId(snowFlake.nextId());
        if (news.getPublishTime() == null) {
            news.setPublishTime(new Date());
        }
        newsMapper.insert(news);
    }

    public void update(NewsRequest request) {
        News news = BeanUtil.copyProperties(request, News.class);
        // 使用updateByPrimaryKeyWithBLOBs以更新完整内容
        newsMapper.updateByPrimaryKeyWithBLOBs(news);
    }

    public void delete(Long id) {
        newsMapper.deleteByPrimaryKey(id);
    }
}
